from openpyxl.worksheet.worksheet import Worksheet
from openpyxl.styles import numbers
from pandas import DataFrame

FORMAT_NUMBER_COMMA = numbers.BUILTIN_FORMATS[3]
FORMAT_NUMBER_00 = numbers.FORMAT_NUMBER_00
FORMAT_PERCENTAGE_00 = numbers.FORMAT_PERCENTAGE_00

class XlsxHeader:
    def  __init__(self,str_value:str,format:str=None) -> None:
        self.value = str_value
        self.format = format

class XlsxDataFrame:
    def __init__(self, headers:list[XlsxHeader] = [], index:list = [], name:str= "") -> None:
        self.headers = headers
        self.name = name
        self.data:DataFrame = DataFrame(data=0,columns=[h.value for h in self.headers],index=index)
        self.r_map:dict[str,list[int]] = {}
        self.h_map:dict[str,list[int]] = {}
        for i in range(len(self.data.columns)):
            h = self.data.columns[i]
            if(h not in self.h_map):
                self.h_map[h] = []
            self.h_map[h].append(i)

        for i in range(len(self.data.index)):
            r = self.data.index[i]
            if(r not in self.r_map):
                self.r_map[r] = []
            self.r_map[r].append(i)

    def get_row_index(self, r, index:int=0):
        return self.r_map[r][index]

    def get_col_index(self, h, index:int=0):
        return self.h_map[h][index]
    
    def write_as_row(self, out_sheet:Worksheet, row:int=None, col:int=1, write_row_index=True):
        '''第一行为表头，每行一个条目'''
        row_index, col_index = [row, col]
        if(not row_index):
            row_index = out_sheet.max_row + 1

        if(write_row_index):
            out_sheet.cell(row=row_index,column=col_index,value=self.name)
            for j in range(self.data.shape[1]):
                out_sheet.cell(row=row_index,column=col_index+j+1,value=self.data.columns[j])
        else:
            for j in range(self.data.shape[1]):
                out_sheet.cell(row=row_index,column=col_index+j,value=self.data.columns[j])

        row_index += 1

        if(write_row_index):
            for i in range(self.data.shape[0]):
                out_sheet.cell(row=row_index+i,column=col_index,value=self.data.index[i])
                for j in range(self.data.shape[1]):
                    out_cell = out_sheet.cell(row=row_index+i,column=col_index+j+1,value=self.data.iloc[i,j])
                    if(j< len(self.headers) and self.headers[j].format):
                        out_cell.number_format = self.headers[j].format
        else:
            for i in range(self.data.shape[0]):
                for j in range(self.data.shape[1]):
                    out_cell = out_sheet.cell(row=row_index+i,column=col_index+j,value=self.data.iloc[i,j])
                    if(j< len(self.headers) and self.headers[j].format):
                        out_cell.number_format = self.headers[j].format
            
    
    def write_as_col(self, out_sheet:Worksheet, row:int=None,col:int=1, write_row_index=True):
        '''第一列为表头，每列一个条目'''
        row_index, col_index = [row, col]
        if(not row_index):
            row_index = out_sheet.max_row + 1

        if(write_row_index):
            out_sheet.cell(row=row_index,column=col_index,value=self.name)
            for i in range(self.data.shape[1]):
                out_sheet.cell(row=row_index+i+1,column=col_index,value=self.data.columns[i])
        else:
            for i in range(self.data.shape[1]):
                out_sheet.cell(row=row_index+i,column=col_index,value=self.data.columns[i])

        col_index += 1

        if(write_row_index):
            for i in range(self.data.shape[0]):
                out_sheet.cell(row=row_index,column=col_index+i,value=self.data.index[i])
                for j in range(self.data.shape[1]):
                    out_cell = out_sheet.cell(row=row_index+j+1,column=col_index+i,value=self.data.iloc[i,j])
                    if(j< len(self.headers) and self.headers[j].format):
                        out_cell.number_format = self.headers[j].format
        else:
            for i in range(self.data.shape[0]):
                for j in range(self.data.shape[1]):
                    out_cell = out_sheet.cell(row=row_index+j,column=col_index+i,value=self.data.iloc[i,j])
                    if(j< len(self.headers) and self.headers[j].format):
                        out_cell.number_format = self.headers[j].format

